<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/3 0003
  Time: 9:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="title" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入公告标题" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="content" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入公告内容" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">起始时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="createTime1" id="createTime1" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入起始时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="createTime2" id="ncreateTime2" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入结束时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-normal layui-btn-radius layui-btn-sm"  lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>
</form>
<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="addOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题:</label>
            <div class="layui-input-black" style="padding: 5px">
                <input type="hidden" name="newsId">
                <input type="text" name="title" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入标题" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容:</label>
            <div class="layui-input-black" style="padding: 5px">
            <textarea id="content" name="content" style="display: none"></textarea>
        </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>
<%--查看公告的div开始--%>
<div id="viewNewsDiv" style="padding: 10px;display: none">
    <h2 id="view_title" align="center"></h2>
    <hr>
    <div style="text-align: right">
        发布人:<span id="view_openName"></span>
        <span style="display: inline-block;width: 20px"></span>
        发布时间:<span id="view_createTime"></span>
    </div>
    <hr>
    <div id="view_content"></div>
</div>
<%--查看公告的div结束--%>
<%--显示列表--%>
<table id="newsTable" lay-filter="newsTable"></table>

<script id="toolbarNews" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="newsAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="newsDelChk">批量删除</button>
    </div>
</script>

<script id="barNews" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="show">查看</a>
</script>
<script src="/layui/layui.js"></script>
<script>
    var newsTable
    var index
    var url
    var layeditContent
    layui.use(['table','form','layer','jquery','laydate','layedit'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery
        var laydate=layui.laydate
        var layedit=layui.layedit
        laydate.render({
            elem:'#createTime1'
            ,type:'datetime'
        });
        laydate.render({
            elem:'#createTime2'
            ,type:'datetime'
        });
        // 初始table
        newsTable = table.render({
            elem: "#newsTable"
            , url: "/news/findNews"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbarNews'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'newsId', title: '公告ID', fixed: 'center'}
                , {field: 'title', title: '公告标题', fixed: 'center'}
                , {field: 'content', title: '公告内容', fixed: 'center'}
                , {field: 'createTime', title: '发布时间', fixed: 'center'}
                , {field: 'publisherName', title: '发布人', fixed: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#barNews', width: 250}
            ]]
        })
        // 查询
        form.on("submit(formDemo)", function (data) {
            newsTable.reload({
                url: "/news/findNews"
                , where: {
                    'title':data.field.title
                    ,'content':data.field.content
                    ,'createTime1':data.field.createTime1
                    ,'createTime2':data.field.createTime2

                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(newsTable)',function(obj) {
            if(obj.event=="newsDelChk"){
                newsDelChk(obj)
            }else if(obj.event=="newsAdd"){
                newsAdd();
            }
        })
        //监听行工具事件
        table.on('tool(newsTable)', function(obj){
            var data = obj.data
            if(obj.event == "del"){
                del(data)
            }else if(obj.event=="edit"){
                newsUpdate(data)
            }else if(obj.event=="show"){
                show(data)
            }
        })
        function show(data) {
            index = layer.open({
                type: 1,
                title: '查看公告',
                content: $("#viewNewsDiv"),
                area: ['700px', '500px'],
                success: function (index) {
                    $("#view_title").html(data.title);
                    $("#view_openName").html(data.publisherName);
                    $("#view_createTime").html(data.createTime);
                    $("#view_content").html(data.content);
                }
            });
        }

        function del(data){
            layer.confirm("您确定要删除"+ data.title +"公告么？",function(ind){

                $.post("/news/delNews", {"newsId":data.newsId} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    newsTable.reload()

                })
                layer.close(ind)
            })

        }
        function newsDelChk(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var data=checkStatus.data
            if(data.length==0){
                layer.msg("请至少选择一个数据")
            }else {
                var ids=new Array()
                $.each(data,function (index,item) {
                    ids.push(item.newsId)
                })
                layer.confirm("你确定要删除"+data.length+"条数据么？",function (ind) {
                    $.post("/news/newsDelChk",{"ids":ids},function (r) {
                        layer.msg(r.msg)
                        newsTable.reload()
                    })
                    layer.close(ind)
                })
            }
        }
        function newsUpdate(data) {
            index =  layer.open({
                type:1
                , title:"修改公告"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    layeditContent=layedit.build('content')
                    form.val("dataFrm",data)
                    layedit.setContent(layeditContent,data.content)
                    url="/news/newsUpdate"
                }
            })
        }
        function newsAdd() {
            // 弹出框
            index =  layer.open({
                type:1 //
                , title:"添加公告"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    layeditContent=layedit.build('content')
                    url="/news/newsAdd"
                }
            })

        }
        // form表达提交
        form.on("submit(doSubmit)",function() {
            layedit.getContent(layeditContent)
            layedit.sync(layeditContent)
            layer.confirm("您确定要提交该数据么？", function (ind) {
                $.post(url, $("#dataFrm").serialize(), function (data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    newsTable.reload()
                })
                layer.close(ind)
            })

        })
    })
</script>
</body>
</html>
